/**
 * Copyright © 2018 organization baomidou
 * <pre>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * <pre/>
 */
package org.luxor.commons.jdbc.plugin.dynamic.provider;

import org.luxor.commons.jdbc.plugin.dynamic.properties.DataSourceProperty;
import org.luxor.commons.jdbc.plugin.dynamic.properties.DynamicDataSourceProperties;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Map;

/**
 * YML数据源提供者
 *
 * @author TaoYu Kanyuxia
 * @since 1.0.0
 */
public class YmlDynamicDataSourceProvider extends AbstractDataSourceProvider implements DynamicDataSourceProvider {

    /**
     * 多数据源参数
     */
    private final DynamicDataSourceProperties dynamicDataSourceProperties;

    /**
     * Spring Boot 默认数据源
     */
    @Resource
    private DataSourceProperty dataSourceProperty;

    public YmlDynamicDataSourceProvider(DynamicDataSourceProperties properties) {
        this.dynamicDataSourceProperties = properties;
    }

    @Override
    public Map<String, DataSource> loadDataSources() {
        Map<String, DataSourceProperty> dataSourcePropertiesMap = dynamicDataSourceProperties.getDatasources();
        String primary = dynamicDataSourceProperties.getPrimary();
        if (!dataSourcePropertiesMap.containsKey(primary)) {
            dataSourcePropertiesMap.put(primary, dataSourceProperty);
        }
        return createDataSourceMap(dataSourcePropertiesMap);
    }
}
